package com.mrjy.live.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.WisPayment;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.WisInfo;
import com.mrjy.common.entity.WisLessonInfo;
import com.mrjy.common.entity.WisSign;
import com.mrjy.common.util.CharSetUtil;
import com.mrjy.live.dao.IWisPaymentDao;
import com.mrjy.live.dao.IWisDocDao;
import com.mrjy.live.dao.IWisLessonInfoDao;
import com.mrjy.live.dao.IWisSignDao;

@Service("wisLessonInfoBo")
public class WisLessonInfoBo implements IWisLessonInfoBo{
	@Resource
	private IWisLessonInfoDao wisLessonInfoDao;

	@Resource
	private IWisPaymentDao wisPaymentDao;
	@Resource
	private IWisInfoBo wisInfoDao;
	@Resource
	private IWisDocDao wisDocDao;
	
	@Resource
	private IWisSignDao wisSignDao;
	
	
	@Override
	public Map<String, Object> queryLessons(WisLessonInfo wisLessonInfo,
			UserInfo userInfo) {
		Map<String,Object> map = new HashMap<String, Object>();
		boolean flag=true;
		List<WisLessonInfo> lessonList =wisLessonInfoDao.queryLessons(wisLessonInfo);	
		WisSign wisSign=new WisSign();
		if(userInfo==null){
			flag=false;
		}else{			
			wisSign.setUserId(userInfo.getId());
		}
		
		WisSign signNum=new WisSign();
		for(int i = 0; i < lessonList.size(); i++){
			wisSign.setWisLessonId(lessonList.get(i).getId());
			signNum.setWisLessonId(lessonList.get(i).getId());		
			lessonList.get(i).setSignNum(wisSignDao.countWisSign(signNum));
			if(flag){
				WisSign exitstSign=wisSignDao.queryByWisSign(wisSign);
				if(exitstSign != null ){
					lessonList.get(i).setSigned(true);
				}
			}
			
		}
		map.put("total", wisLessonInfoDao.countLessons(wisLessonInfo));
		map.put("rows", lessonList);
		return map;
	}
	
	@Override
	public Object addLesson(WisLessonInfo wisLessonInfo, UserInfo userInfo) {
		wisLessonInfo.setTeacherId(userInfo.getId());
		wisLessonInfo.setLessonName(CharSetUtil.toUTF8(wisLessonInfo.getLessonName()));
		wisLessonInfo.setLessonDesc(CharSetUtil.toUTF8(wisLessonInfo.getLessonDesc()));
		wisLessonInfo.setDisableFlag(0);
		Date startTime = new Date(wisLessonInfo.getStartTimeSec());
		Date finishedTime = new Date(wisLessonInfo.getFinishedTimeSec());	
		wisLessonInfo.setStartTime(startTime);
		wisLessonInfo.setFinishedTime(finishedTime);
		wisLessonInfo.setTheTime(startTime);
		wisLessonInfo.setLessonCode(userInfo.getLoginCode() + "_" + new Date().getTime());
		wisLessonInfo.setTopicCode("topic"+userInfo.getId()+new Date().getTime());
		int addCount = wisLessonInfoDao.insert(wisLessonInfo);
		if(addCount > 0){
			if(!"0".equals(wisLessonInfo.getWisCode())){
				WisInfo wisInfo=new WisInfo();
				wisInfo.setTeacherId(userInfo.getId());
				wisInfo.setWisCode(wisLessonInfo.getWisCode());
				wisInfoDao.insertWisInfo(wisInfo);
				}
			return "ok";
		/*WisLessonInfo existWisLesson = wisLessonInfoDao.queryByLessonName(wisLessonInfo.getLessonName());
		if(existWisLesson != null){
			wisLessonInfo.setLessonCode(existWisLesson.getLessonCode());
			if("".equals(existWisLesson.getTopicCode())){
			wisLessonInfo.setTopicCode("topic"+userInfo.getId()+new Date().getTime());	
			}
			int updateCount = wisLessonInfoDao.updateByCode(wisLessonInfo);
			if(updateCount > 0){
				if(!"0".equals(wisLessonInfo.getWisCode())){
				WisInfo wisInfo=new WisInfo();
				wisInfo.setTeacherId(userInfo.getId());
				wisInfo.setWisCode(wisLessonInfo.getWisCode());
				wisInfoDao.insertWisInfo(wisInfo);
				}
				return "ok";
			}
		}else{*/
			
			//}
		}
		
		return null;
	}
	
	@Override
	public Object deleteLessonById(WisLessonInfo wisLessonInfo) {
		int deleteCount = wisLessonInfoDao.deleteByPrimaryKey(wisLessonInfo.getId());
		if(deleteCount > 0){
			return "ok";
		}
		return null;
	}
	
	@Override
	public Map<String, Object> queryLessonByTeacherId(WisLessonInfo wisLessonInfo, UserInfo userInfo) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(wisLessonInfo.getTeacherId() == null){
			wisLessonInfo.setTeacherId(userInfo.getId());
		}
		if(wisLessonInfo.getStartTimeSec()!=null){
		Date startTime = new Date(wisLessonInfo.getStartTimeSec());
		wisLessonInfo.setStartTime(startTime);
		}
		if(wisLessonInfo.getFinishedTimeSec()!=null){
		Date finishedTime = new Date(wisLessonInfo.getFinishedTimeSec());			
		wisLessonInfo.setFinishedTime(finishedTime);
		}
		List<WisLessonInfo> lessonList =  wisLessonInfoDao.queryByTeacherId(wisLessonInfo);
		//WisPayment WisPayment = new WisPayment();
		//WisPayment.setUserId(userInfo.getId());
		WisSign wisSign=new WisSign();
		boolean flag=true;
		if(userInfo==null){
			flag=false;
		}else{
			wisSign.setUserId(userInfo.getId());
		}
		
		
		WisSign signNum=new WisSign();
		for(int i = 0; i < lessonList.size(); i++){
			
			signNum.setWisLessonId(lessonList.get(i).getId());		
			lessonList.get(i).setSignNum(wisSignDao.countWisSign(signNum));
			if(flag){
				if(userInfo.getRoleId()==2){
					lessonList.get(i).setSigned(true);
				}else{
					wisSign.setWisLessonId(lessonList.get(i).getId());
					WisSign exitstSign=wisSignDao.queryByWisSign(wisSign);
					if(exitstSign != null){
						lessonList.get(i).setSigned(true);
					}
				}
			}
			
			/*if(lessonList.get(i).getPrice() > 0){
				//WisPayment.setLiveInfoId(lessonList.get(i).getId());				
				//WisPayment existPayment = wisPaymentDao.queryPayment(WisPayment);				
				//if(existPayment != null){
				//	lessonList.get(i).setPayed(true);
				//}			
				WisSign exitstSign=wisSignDao.queryByWisSign(wisSign);
				if(exitstSign != null){
					lessonList.get(i).setSigned(true);
				}
			}else{
				//lessonList.get(i).setPayed(true);
				WisSign exitstSign=wisSignDao.queryByWisSign(wisSign);
				if(exitstSign != null){
					lessonList.get(i).setSigned(true);
				}
			}*/
		}
		map.put("total", lessonList.size());
		map.put("rows", lessonList);
		return map;
	}

	@Override
	public void txDeleteLessonByLessonCode(WisLessonInfo wisLessonInfo) {
		WisLessonInfo wisInfo = wisLessonInfoDao.queryByLessonCode(wisLessonInfo.getLessonCode());
		//wisPaymentDao.deleteAllByliveInfoId(wisInfo.getId());
		WisPayment payment=new WisPayment();
		payment.setLiveInfoId(wisInfo.getId());
		int count=wisPaymentDao.countLivePayment(payment);
		if(count>0){
			wisLessonInfo.setDisableFlag(1);
			wisLessonInfoDao.updateByCode(wisLessonInfo);
		}else{
		wisDocDao.deleteByLessonId(wisInfo.getId());
		wisLessonInfoDao.deleteByCode(wisLessonInfo.getLessonCode());
		}
	}

	@Override
	public Object updateLessonByCode(WisLessonInfo wisLessonInfo) {
		wisLessonInfo.setLessonName(CharSetUtil.toUTF8(wisLessonInfo.getLessonName()));
		wisLessonInfo.setLessonDesc(CharSetUtil.toUTF8(wisLessonInfo.getLessonDesc()));
		Date finishedTime = new Date(wisLessonInfo.getFinishedTimeSec());
		Date startTime = new Date(wisLessonInfo.getStartTimeSec());
		wisLessonInfo.setStartTime(startTime);
		wisLessonInfo.setFinishedTime(finishedTime);
		wisLessonInfo.setTheTime(startTime);
		int updateCount = wisLessonInfoDao.updateByCode(wisLessonInfo);
		if(updateCount > 0){
			return "ok";
		}
		return null;
	}

	@Override
	public Map<String, Object> queryTeachers(
		WisLessonInfo wisLessonInfo) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		List<WisLessonInfo> teacherList =  wisLessonInfoDao.queryTeachers(wisLessonInfo);
		map.put("total", wisLessonInfoDao.countTeachers(wisLessonInfo));
		map.put("rows", teacherList);
		return map;
	}

	@Override
	public Object updateOnlinePeople(WisLessonInfo wisLessonInfo) {
		int updateCount = wisLessonInfoDao.updateOnlinePeopleByCode(wisLessonInfo);
		if(updateCount > 0){
			return "ok";
		}
		return null;
	}
	
	
	@Override
	public Object queryByLessonCode(WisLessonInfo wisLessonInfo) {
		return wisLessonInfoDao.queryByLessonCode(wisLessonInfo.getLessonCode());
	}

	@Override
	public Object queryLimitByLessonCode(WisLessonInfo wisLessonInfo) {
		return wisLessonInfoDao.queryLimitByLessonCode(wisLessonInfo.getLessonCode());
	}

	@Override
	public Object checkRoomTime(WisLessonInfo wisLessonInfo) {
		WisLessonInfo wis = wisLessonInfoDao.queryByLessonCode(wisLessonInfo.getLessonCode());
		Long serverTime = wisLessonInfoDao.getTime().getTime();
		Long filishTime = wis.getFinishedTime().getTime();
		Long startTime = wis.getStartTime().getTime();
		if((serverTime.longValue() + 600000) < startTime){
			return 2;
		}
		if(wis.getOnFlag()==1){
			return 1;
		}
		if(serverTime.longValue() > filishTime.longValue()){
			wisLessonInfo.setOnFlag(1);
			wisLessonInfoDao.updateByCode(wisLessonInfo);
			wisDocDao.deleteByLessonId(wis.getId());
			return 1;
		}else{
			if(wisLessonInfo.getRoleId() == 2){
				if(serverTime.longValue() < startTime){
					return wis.getOnFlag() + "#" + wis.getTheTime().getTime() + "#" + serverTime.longValue();
				}else{
					if(wis.getTheTime().getTime() > wis.getStartTime().getTime()){
						return wis.getOnFlag() + "#" + wis.getTheTime().getTime() + "#" + serverTime.longValue();
					}else{
						wisLessonInfo.setTheTime(wisLessonInfoDao.getTime());
						wisLessonInfoDao.updateByCode(wisLessonInfo);
						return wis.getOnFlag() + "#" + serverTime.longValue() + "#" + serverTime.longValue();
					}
				}
			}else{
				return wis.getOnFlag() + "#" + wis.getTheTime().getTime() + "#" + serverTime.longValue();
			}
		}
	}

}
